package org.zjx.message.messages;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.zjx.message.AbstractMessage;

@EqualsAndHashCode(callSuper = true)
@Data
@NoArgsConstructor
public class ResponseMessage extends AbstractMessage {
    private int code;
    private String message;
    private Object data;
    private String tag;

    public ResponseMessage(String messageId) {
        setMessageId(messageId);
        setTimestamp(System.currentTimeMillis());
    }

    public static ResponseMessage success(CommandMessage cmd, Object data) {
        ResponseMessage resp = new ResponseMessage(cmd.getMessageId());
        resp.setCode(0);
        resp.setTag(cmd.getCommand());
        resp.setMessage("Success");
        resp.setData(data);
        return resp;
    }

    public static ResponseMessage error(CommandMessage cmd, String error) {
        ResponseMessage resp = new ResponseMessage(cmd == null ? "" : cmd.getMessageId());
        resp.setCode(-1);
        if (cmd != null) resp.setTag(cmd.getCommand());
        resp.setMessage(error);
        return resp;
    }
}